<script setup lang="ts">
import { RegisterAPI } from '@/apis/user';
import router from '@/router';
import { useUserStore } from '@/stores';
import { RuleMobile, passwordRules, RulesCode } from '@/utils/rules'
import { showFailToast, showToast } from 'vant';
import { ref } from 'vue'
import { useRoute } from 'vue-router'
const route = useRoute()
const password = ref('')
const isAgree = ref(false)
const show = ref(true)
const code = ref('')
const mobile = ref('')
//前往注册
//登录表单提交
const onSubmit = async () => {
  if (!isAgree.value) return showFailToast('请勾选我已同意')
  const res = await RegisterAPI(mobile.value, password.value, code.value)
  const store = useUserStore()//拿state来接函数useUserStore
  store.setUser(res.data)
  // console.log(res.data);
  showToast('注册成功')
  router.replace((route.query.returnUrl as string) || '/')
}
//发送验证码
import { useMobileCode } from '@/composables/useMobileCode'
const { sendCode, time, form } = useMobileCode(mobile, 'register')
</script>

<template>
  <div class="register-page">
    <cp-nav-bar right-text="?去登录" @click-right="router.push('/login')"></cp-nav-bar>
    <!-- 头部 -->
    <div class="register-head">
      <h3>注册</h3>
    </div>
    <!-- 表单 -->
    <van-form autocomplete="off" @submit="onSubmit" ref="form">
      <van-field v-model="mobile" name="mobile" :rules="RuleMobile" placeholder="请输入手机号" type="tel"></van-field>
      <van-field v-model="password" :rules="passwordRules" placeholder="请输入密码" :type="show ? 'text' : 'password'">
        <template #button>
          <cp-icon @click="show = !show" :name="`login-eye-${show ? 'on' : 'off'}`"></cp-icon>
        </template>
      </van-field>
      <van-field v-model="code" :rules="RulesCode" placeholder="短信验证码">
        <template #button>
          <span class="btn-send" @click="sendCode" :class="{ active: time }">{{ time > 0 ? `${time}后再发送` : '发送验证码'
            }}</span>
        </template>
      </van-field>
      <div class="cp-cell">
        <van-checkbox v-model="isAgree">
          <span>我已同意</span>
          <a href="javascript:;">用户协议</a>
          <span>及</span>
          <a href="javascript:;">隐私条款</a>
        </van-checkbox>
      </div>
      <div class="cp-cell">
        <van-button block round type="primary" native-type="submit">注 册</van-button>
      </div>
    </van-form>
  </div>
</template>

<style lang="scss" scoped>
.register {
  &-page {
    padding-top: 46px;
  }

  &-head {
    display: flex;
    padding: 30px 30px 50px;
    justify-content: space-between;
    align-items: flex-end;
    line-height: 1;

    h3 {
      font-weight: normal;
      font-size: 24px;
    }

    a {
      font-size: 15px;
    }
  }

  &-other {
    margin-top: 60px;
    padding: 0 30px;

    .icon {
      display: flex;
      justify-content: center;

      img {
        width: 36px;
        height: 36px;
        padding: 4px;
      }
    }
  }
}

.van-form {
  padding: 0 14px;

  .cp-cell {
    height: 52px;
    line-height: 24px;
    padding: 14px 16px;
    box-sizing: border-box;
    display: flex;
    align-items: center;

    .van-checkbox {
      a {
        color: var(--cp-primary);
        padding: 0 5px;
      }
    }
  }

  .btn-send {
    color: var(--cp-primary);

    &.active {
      color: rgba(22, 194, 163, 0.5);
    }
  }
}
</style>